//
//  AppDelegate.m
//  KEREN
//
//  Created by MW on 2023/3/8.
//

#import "AppDelegate.h"
#import "LoginViewController.h"
#import "KRTabarController.h"
@interface AppDelegate ()<UIApplicationDelegate,WXApiDelegate,HABleManagerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = Main_BackColor;
    [self configure];
    [self configLog];
    UserInfo *user = USERINFO;
    if(user.token.length > 0){
        [self gotoHome];
    }else{
        [self gotoLogin];
    }
    HABleManager *bleManager = [HABleManager sharedManager];
    bleManager.delegate = self;
    return YES;
}

- (void)gotoLogin{
    LoginViewController *vc = [[LoginViewController alloc] init];
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = navVc;
}

- (void)gotoHome{
    KRTabarController *vc = [[KRTabarController alloc] init];
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
    [UIApplication sharedApplication].delegate.window.rootViewController = navVc;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    return  [WXApi handleOpenURL:url delegate:self];

}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler{
    return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}

- (void)onResp:(BaseResp *)resp{
    //微信登录
    if ([resp isKindOfClass:[SendAuthResp class]]) {
        [[LoginManager sharedInstance] getWXAuthResp:(SendAuthResp *)resp];
    }else{
    }
}

//视频进入后台到前台视频暂停的处理方法
- (void)applicationDidBecomeActive:(UIApplication *)application {
    //_weakself;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter]postNotificationName:@"appBecomeActive" object:nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"AVCaptureTorchModeOffSDT" object:nil];
    });
    
}

-(void)changePeriheralStatus{
    [[NSNotificationCenter defaultCenter] postNotificationName:BlueToothStatePoweredOff object:nil];
}

#pragma mark- centralHAManagerDidUpdateState
-(void)centralHAManagerDidUpdateState:(CBCentralManager *)central{
    if(central.state == CBManagerStatePoweredOn){
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            DeviceInfo *currentDeviceInfo = [UserInfoManager sharedInstance].currentDeviceInfo;
            if(currentDeviceInfo){
                CBPeripheral *peripheral = [HABleManager checkBleConnectedPeripheral:currentDeviceInfo.sn_code];
                DDLogInfo(@"=======centralHAManagerDidUpdateState peripheral is %@,authorized is %@,sn_code is %@",peripheral,peripheral.authorized,currentDeviceInfo.sn_code);
                if(peripheral){
                    if(peripheral.state != CBPeripheralStateConnected){
                        DDLogInfo(@"=======bleConnected characteristic is: 06");
                        [KeRenBleTool bleConnected:peripheral callBack:nil];
                    }
                }else{
                    [KeRenBleTool scanAndConnectedBLE:[UserInfoManager sharedInstance].currentDeviceInfo.sn_code callBack:nil];
                }
            }
        });
    }else if(central.state == CBManagerStatePoweredOff){
        [self performSelector:@selector(changePeriheralStatus) afterDelay:1.5];
    }
}

@end
